<?php

namespace mpend\modules\home\models;

use common\helpers\ArrayHelper;
use common\models\actor\Actor;
use Yii;
use common\helpers\Url;
use common\logics\PositionLogic;
use mpend\models\searchs\AssembleProductSearch;
use common\models\notice\NoticeTag;
use common\models\notice\Notice;
use common\models\notice\NoticeNeed;
use common\models\notice\ShowbizRole;
use yii\db\Expression;

/**
 * 猜你喜欢查询模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ThinksSearch extends \common\base\Model
{

    public $user_id;

    public function recommenders($length = 4)
    {
        //简历关键词
        $actor = Actor::find()->alias('t')->active()->andWhere([
            't.user_id' => $this->user_id
        ])->one();
        $roleIds = [];
        if ($actor) {
            $tags = $actor->tagNames;
            $keywords = ShowbizRole::find()->active()->andWhere([
                'name' => $tags
            ])->all();
            $roleIds = ArrayHelper::getColumn($keywords, 'id');
        }
//

        //$roleIds = [1,2,3,4];
        //$length = count($roleIds);
        //
        //return ArrayHelper::uniqueCombinationToString($roleIds, $length-1);
        //$data = $query->asArray()->all();
        //$temp = [];
        //$keywords = [];
        //foreach ($data as $item) {
        //    $words = explode(',', $item['tagnames']);
        //    sort($words);
        //    $reals = implode(',', $words);
        //    if (isset($temp[$reals])) {
        //        $temp[$reals]++;
        //    } else {
        //        $temp[$reals] = 0;
        //    }
        //    foreach ($words as $word) {
        //        if (isset($keywords[$word])) {
        //            $keywords[$word]++;
        //        } else {
        //            $keywords[$word] = 1;
        //        }
        //    }
        //}
        //$likes = array_filter($temp);
        //return [
        //    $likes,
        //    $keywords,
        //];
        return $this->queryNotice($roleIds);
    }

    public function queryNotice($roleIds)
    {
        $query = Notice::find()->alias('t');
        $query->andWhere([
            't.status' => 10
        ]);
        $query->joinWith('needRoles needRoles');
        $query->andFilterWhere([
            'needRoles.role_id' => $roleIds
        ]);
        $query->groupBy([
            't.id'
        ]);
        $query->orderBy([
            't.created_at' => SORT_DESC
        ]);
        $query->limit(4);
        return $query->all();
    }

}
